package psbc.daily.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import psbc.daily.context.BaseContext;
import psbc.daily.entity.MyLog;
import psbc.daily.entity.dto.SearchDto;
import psbc.daily.service.MyLogService;

import java.text.ParseException;
import java.util.List;

@RequestMapping("/mylog")
@RestController
@Slf4j
public class MyLogController {
    @Autowired
    private MyLogService myLogService;

    /**
     * 新增日志
     * @param record 日志对象
     * @return 影响行数
     */
    @PostMapping("/add")
    @CrossOrigin
    public int addRecord(@RequestBody MyLog record) {
        log.info("新增日志数据：{}",record);
        return myLogService.addLog(record);
    }

    /**
     * 根据id查询日志
     * @param id 日志id
     * @return 日志对象
     */
    @PostMapping("/selectById")
    public MyLog selectById(@RequestBody int id) {
        return myLogService.selectById(id);
    }

    /**
     * 查询所有日志
     * @return 日志集合
     */
    @GetMapping("/selectAll")
    @CrossOrigin
    public List<MyLog> selectAll() {
        Long userId = BaseContext.getCurrentId();
        return myLogService.getAll(userId);
    }

    /**
     * 查询日志
     * @param searchDto 查询参数
     * @return
     */
    @PostMapping("/selectByDate")
    @CrossOrigin
    public List<MyLog> selectByDate(@RequestBody SearchDto searchDto) throws ParseException {
        log.info("查询参数:{}",searchDto);
        Long currentId = BaseContext.getCurrentId();
        return myLogService.selectByDate(searchDto,currentId);
    }

    @PostMapping("/deleteByDate")
    public int deleteByDate(@RequestBody String recordDate) {
        return myLogService.deleteByDate(recordDate);
    }

    @PostMapping("/deleteById/{ids}")
    @CrossOrigin
    public int deleteById(@PathVariable int[] ids) {
        log.info("删除日志id:{}",ids);
        for (int id : ids) {
            myLogService.deleteById(id);
        }
        return 1;

    }

    /**
     * 修改日志
     * @param myLog 日志对象
     * @return 影响行数
     */
    @PostMapping("/updateRecord")
    @CrossOrigin
    public int updateRecord(@RequestBody MyLog myLog) {
        return myLogService.updateLog(myLog);
    }
}
